cc.Class({
    extends: cc.Component,

    properties: {
        AreaIDLB:cc.Label,
       AreaLine:{
           get(){
               if(!this._AreaLine)this._AreaLine=12;
               return this._AreaLine;
           },
           set(value){
               this._AreaLine=value;
           }
       },
       AreaRow:{
           get()
           {
               if(!this._AreaRow)this._AreaRow=20;
               return this._AreaRow;
           },
           set(value)
           {
               this._AreaRow=value;
           }
       },
       CellWidth:{
           get()
           {
               if(!this._CellWidth)this._CellWidth=64;
               return this._CellWidth;
           },
           set(value)
           {
               this._CellWidth=value;
           }
       },
       CellHeight:
       {
           get()
           {
               if(!this._CellHeight)this._CellHeight=64;
               return this._CellHeight;
           },
           set(value)
           {
               this._CellHeight=value;
           }
       },
       CurrentMapID:
       {
           get()
           {
               if(!this._CurrentMapID)this._CurrentMapID=0;
               return this._CurrentMapID;
           },
           set(value)
           {
               this._CurrentMapID=value;
               this.AreaIDLB.string=value;
           }
       },
       SelectTitled:
       {
           get(){
               return this._SelectTitled;
           },
           set(value)
           {
               this._SelectTitled=value;
           }
       },
       OnStateChange:
       {
           get(){
               return this._OnStateChange;
           },
           set(value)
           {
               this._OnStateChange=value;
           }
       }
    },

    // LIFE-CYCLE CALLBACKS:
    onLoad () 
    {
        var loadFinish = function(error,resource)
        {
            if(!resource) 
            {
                cc.error("Load LoadPrefab Error:"+error);
                return ;
            }
            for(var lidx=0;lidx<this.AreaLine;lidx++)
            {
                for(var ridx=0;ridx<this.AreaRow;ridx++)
                {
                    var x=(ridx*this.CellWidth+0.5*this.CellWidth)-(this.CellWidth*this.AreaRow*0.5);
                    var y=(this.CellHeight*this.AreaLine*0.5)-(lidx*this.CellHeight+0.5*this.CellHeight);
                    var obj = cc.instantiate(resource);
                    obj.setPosition(cc.v2(x,y));
                    obj.name=lidx+"_"+ridx;
                    if(!obj.getComponent("UI-TiledFiled"))obj.addComponent("UI-TiledFiled");
                    obj.getComponent("UI-TiledFiled").OnStateChange=this.OnStateChange;
                    this.node.getChildByName("TiledNodeRoot").addChild(obj);
                }
            }
        }
            cc.loader.loadRes("./prefab/TiledMap/UI-TiledFiled",(completedCount,totalCount,item)=>{},loadFinish.bind(this));
        
    },
    UpdateMapAreaData(data)
    {
        for(var lidx=0;lidx<this.AreaLine;lidx++)
        {
            for(var ridx=0;ridx<this.AreaRow;ridx++)
            {
                var objName = lidx+"_"+ridx;
                var nd = this.node.getChildByName("TiledNodeRoot").getChildByName(objName);
                var TData= data.Data[lidx][ridx];
                var comp = nd.getComponent("UI-TiledFiled");
                comp.OnStateChange=this.OnStateChange;
                comp.UpdateTiledData(TData);
            }
        }
        this.AreaIDLB.string=data.MapID;
        this.SelectTitled=undefined;
    },
    SelectTiledItem(xPos,yPos)
    {
        if(this.SelectTitled)
        {
            var comp = nd.getComponent("UI-TiledFiled");
            if(comp)comp.SetSelectTiledState(false);
        }
        var objName = xPos+"_"+yPos;
        var nd = this.node.getChildByName("TiledNodeRoot").getChildByName(objName);
        if(nd)
        {
            this.SelectTitled=nd;
            var comp = nd.getComponent("UI-TiledFiled");
            if(comp)comp.SetSelectTiledState(true);
        }
    },
    start () {

    },

    // update (dt) {},
});
